package cn.wenhe9.yygh.common.utils;

import cn.wenhe9.yygh.common.helper.JwtHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * @author DuJinliang
 * 2022/4/5
 * 获取当前用户信息的工具类
 */
public class AuthContextHolder {
    /**
     * 获取当前用户id
     * @param request HttpServletRequest
     * @return Long
     */
    public static Long getUserId(HttpServletRequest request){
        //从header中获取token
        String token = request.getHeader("token");
        //jwt从token中获取userId
        Long userId = JwtHelper.getUserId(token);
        return userId;
    }

    /**
     * 获取当前用户名称
     * @param request HttpServletRequest
     * @return String
     */
    public static String getUsername(HttpServletRequest request){
        //从header中获取token
        String token = request.getHeader("token");
        //jwt从token中获取userId
        String userName = JwtHelper.getUserName(token);
        return userName;
    }
}
